package com.edais.service.imp.gift;

import com.edais.domain.User;
import com.edais.domain.gift.GiftPackageGift;
import com.edais.domain.gift.GiftPackageGiftOrder;
import com.edais.persistence.gift.GiftPackageGiftOrderMapper;
import com.edais.service.gift.GiftPackageGiftOrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 礼包礼品订单ServiceImpl
 * Created by haoyx on 2016/11/15.
 */
@Service
public class GiftPackageGiftOrderServiceImpl implements GiftPackageGiftOrderService {

    private static final Logger logger = Logger.getLogger(GiftPackageGiftOrderServiceImpl.class);

    @Autowired
    private GiftPackageGiftOrderMapper orderMapper;

    @Override
    public void insertOrder(GiftPackageGiftOrder order) {
        this.orderMapper.insert(order);
    }

    @Override
    public GiftPackageGiftOrder selectByPrimaryKey(Long id) {
        return this.orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public GiftPackageGiftOrder selectGiftPackageGiftOrder(Map<String, Object> params) {
        return this.orderMapper.selectGiftPackageGiftOrder(params);
    }

    @Override
    public List<GiftPackageGiftOrder> selectGiftPackageGiftOrderList(Map<String, Object> params) {
        return this.orderMapper.selectGiftPackageGiftOrderList(params);
    }

    @Override
    public int countGiftPackageGiftOrders(Map<String, Object> params) {
        return this.orderMapper.countGiftPackageGiftOrders(params);
    }

    @Override
    public void updateOrderStatus(String logInfo, GiftPackageGiftOrder order, Date nowDate) {
        logger.info(logInfo+order.getId() + order.getStatus() + "更新礼包礼品订单开始");
        order.setUpdatePer("javaservice");
        order.setUpdateTime(nowDate);
        this.orderMapper.updateOrderStatus(order);
        logger.info(logInfo+order.getId() + order.getStatus() + "更新礼包礼品订单成功");
    }

    @Override
    public GiftPackageGiftOrder generateOrder(String logInfo, Long packageOrderId, User user, GiftPackageGift gift, Date nowDate) {

        logger.info(logInfo+"生成礼包礼品订单开始");
        GiftPackageGiftOrder order = new GiftPackageGiftOrder();
        order.setName(gift.getGiftName());
        order.setUrl(gift.getGiftUrl());
        order.setContent(gift.getGiftContent());
        order.setUserId(Long.valueOf(user.getId()));
        order.setUserName(user.getNick_name());
        order.setUserMobile(user.getMobile());
        order.setExchangeDate(nowDate);
        order.setStatus(GiftPackageGiftOrder.STATUS_SUCCESS);
        order.setCreatePer("javaservice");
        order.setCreateTime(nowDate);
        order.setGiftId(gift.getGiftId());
        order.setGiftName(gift.getGiftName());
        order.setPackageId(gift.getPackageId());
        order.setPackageName(gift.getPackageName());
        order.setPackageGiftId(gift.getId());
        order.setPackageOrderId(packageOrderId);
        this.insertOrder(order);
        if(null == order.getId())
            logger.info(logInfo + "生成礼包礼品订单失败");

        logger.info(logInfo+"生成礼包礼品订单成功");
        return order;
    }
}
